package com.dev.print.utils;

import android.support.annotation.NonNull;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.text.DecimalFormat;

public class ParseTools {

    public static byte[] lastBuffer;
    static DecimalFormat df = new DecimalFormat("######0.00");
    static DecimalFormat df2 = new DecimalFormat("######0.0");

//    public static Info getInfo(byte[] buffer) {
//
//        int function = Integer.parseInt(Integer.toHexString(buffer[3] & 0xFF), 16);
//        info.setFunction(function);
//        info.setChargingNum(Integer.parseInt(Integer.toHexString(buffer[6] & 0xFF), 16));
//        info.setPageNum(Integer.parseInt(Integer.toHexString(buffer[7] & 0xFF), 16));
//        info.setPrice(df2.format(Double.parseDouble(Double.toHexString(buffer[8] & 0xFF)) / 10));
//        info.setChargeableTime(Integer.parseInt(Integer.toHexString(buffer[9] & 0xFF), 16));
//        info.setMoney(Integer.parseInt(Integer.toHexString(buffer[10] & 0xFF), 16) / 10);
//        info.setChargingTime(Integer.parseInt(Integer.toHexString(buffer[11] & 0xFF), 16));
//        info.setChargingId(Integer.parseInt(Integer.toHexString(buffer[12] & 0xFF), 16));
//        info.setCountDown(Integer.parseInt(Integer.toHexString(buffer[13] & 0xFF), 16));
//        info.setNowChargingState(Integer.parseInt(Integer.toHexString(buffer[14] & 0xFF), 16));
//        info.setAlreadyChargingHour(Integer.parseInt(Integer.toHexString(buffer[15] & 0xFF), 16));
//        info.setAlreadyChargingMin(Integer.parseInt(Integer.toHexString(buffer[16] & 0xFF), 16));
//        info.setRemainingHour(Integer.parseInt(Integer.toHexString(buffer[17] & 0xFF), 16));
//        info.setRemainingMin(Integer.parseInt(Integer.toHexString(buffer[18] & 0xFF), 16));
//
//
//        info.setElectricity(df.format(Double.parseDouble(Double.toHexString(buffer[19] & 0xFF)) / 100));
//        List<Integer> data = new ArrayList<>();
//        for (int i = 20; i < 270; i++) {
//            data.add(Integer.parseInt(Integer.toHexString(buffer[i] & 0xFF), 16));
//        }
//
//        info.setChargingStates(data);
//        int len = 0;
//        for (int i = 270; i < 299; i += 2) {
//            if (Integer.parseInt(Integer.toHexString(buffer[i] & 0xFF), 16) == 0 && Integer.parseInt(Integer.toHexString(buffer[i + 1] & 0xFF), 16) == 0) {
//                len = i - 270;
//                break;
//            }
//
//        }
//        if (len == 0) {
//            len = 30;
//        }
//        if (len != 0) {
//            byte[] tBytes = new byte[len];
//            System.arraycopy(buffer, 270, tBytes, 0, len);
//            info.setReminder(Byte2Unicode(tBytes));
//        }
//
//
//        return info;
//    }


    public static String Byte2Unicode(byte abyte[], int st, int bEnd) // 不包含bEnd
    {
        StringBuffer sb = new StringBuffer("");
        for (int j = st; j < bEnd; ) {
            int lw = abyte[j++];
            if (lw < 0) lw += 256;
            int hi = abyte[j++];
            if (hi < 0) hi += 256;
            char c = (char) (lw + (hi << 8));
            sb.append(c);
        }

        return sb.toString();
    }

    public static String Byte2Unicode(byte abyte[], int len) {
        return Byte2Unicode(abyte, 0, len);
    }

    public static String Byte2Unicode(byte abyte[]) {
        return Byte2Unicode(abyte, 0, abyte.length);
    }

    public static byte[] Unicode2Byte(String s) {
        int len = s.length();
        byte abyte[] = new byte[len << 1];
        int j = 0;
        for (int i = 0; i < len; i++) {
            char c = s.charAt(i);
            abyte[j++] = (byte) (c & 0xff);
            abyte[j++] = (byte) (c >> 8);
        }

        return abyte;
    }


    public static boolean isSame(byte[] buffer) {

        if (buffer != null && lastBuffer != null) {
            LogUtils.d(ParseTools.byteToString(lastBuffer).toString());
            for (int i = 0; i < buffer.length; i++) {
                if (buffer[i] != lastBuffer[i]) {
                    LogUtils.d("data:isSame" + false);
                    lastBuffer = buffer;
                    return false;
                }
            }
        } else if (lastBuffer == null) {
            lastBuffer = buffer;
            return false;
        }
        LogUtils.d("data:isSame" + true);
        return true;
    }

    @NonNull
    public static StringBuffer byteToString(byte[] data) {
        StringBuffer sb = new StringBuffer();
        for (byte b : data) {
            sb.append(b + ",");
        }
        return sb;
    }


    /**
     * 串口发送数据
     *
     * @param buffer 下发数据内容
     * @return
     */
    public static byte[] gentrateInfo(byte[] buffer) {


        byte[] result = new byte[10];
        byte[] newbuffer = new byte[8];
        System.arraycopy(buffer, 0, newbuffer, 6, 2);// 数据帧;
        newbuffer[0] = (byte) 0x68;

        newbuffer[1] = (byte) 0x00;//版本号
        newbuffer[2] = (byte) 0x01;

        newbuffer[3] = (byte) 0x02;//功能码

        newbuffer[4] = (byte) 0x00;//数据长度
        newbuffer[5] = (byte) 0x02;

        result[8] = checkData(newbuffer);// 校验数据帧;

        System.arraycopy(newbuffer, 0, result, 0, newbuffer.length);// 数据帧;
        result[9] = (byte) 0x16;

        return result;
    }

    public static byte checkData(byte[] buffer) {
        int iNum = 0;
        for (int i = 0; i < buffer.length; i++)
            iNum += buffer[i];
        return (byte) (iNum % 256);
    }


    public static byte[] intToByte(int res) {
        byte[] targets = new byte[4];
        targets[3] = (byte) (res & 0xff);// 最低位 CRC16_H
        targets[2] = (byte) ((res >> 8) & 0xff);// 次低位 CRC16_L
        targets[1] = (byte) ((res >> 16) & 0xff);// 次高位
        targets[0] = (byte) (res >>> 24);// 最高位,无符号右移。
        return targets;
    }

    private char[] bytyToChars(byte[] bytes) {
        Charset cs = Charset.forName("UTF-8");
        ByteBuffer bb = ByteBuffer.allocate(bytes.length);
        bb.put(bytes);
        bb.flip();
        CharBuffer cb = cs.decode(bb);

        return cb.array();
    }

    public static void printToHex(byte[] buffer) {
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < buffer.length; i++) {
            result.append(Integer.toHexString(buffer[i] & 0xFF) + " ");
        }
        System.out.println(result);
    }
}
